#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MySQL快速连接测试工具
用于快速验证韶华墨韵项目的MySQL数据库连接状态
"""

import mysql.connector
from mysql.connector import Error
import sys
import argparse

# 默认数据库配置
DEFAULT_CONFIG = {
    'host': 'localhost',
    'port': 3306,
    'database': 'shufa',
    'user': 'root',
    'password': 'root'
}

def quick_test(config=None):
    """
    快速测试MySQL连接
    """
    if config is None:
        config = DEFAULT_CONFIG
    
    try:
        print(f"测试连接: {config['user']}@{config['host']}:{config['port']}/{config['database']}")
        
        connection = mysql.connector.connect(
            host=config['host'],
            port=config['port'],
            database=config['database'],
            user=config['user'],
            password=config['password'],
            connect_timeout=10
        )
        
        cursor = connection.cursor()
        
        # 基本信息
        cursor.execute("SELECT VERSION(), DATABASE(), @@time_zone")
        version, db, timezone = cursor.fetchone()
        
        # 表数量
        cursor.execute("SHOW TABLES")
        table_count = len(cursor.fetchall())
        
        print(f"✅ 连接成功!")
        print(f"   MySQL版本: {version}")
        print(f"   当前数据库: {db}")
        print(f"   时区: {timezone}")
        print(f"   表数量: {table_count}")
        
        cursor.close()
        connection.close()
        return True
        
    except Error as e:
        print(f"❌ 连接失败: {e.msg}")
        return False
    except Exception as e:
        print(f"❌ 未知错误: {str(e)}")
        return False

def main():
    parser = argparse.ArgumentParser(description='MySQL快速连接测试')
    parser.add_argument('--host', default='localhost', help='MySQL主机地址')
    parser.add_argument('--port', type=int, default=3306, help='MySQL端口')
    parser.add_argument('--database', default='shufa', help='数据库名')
    parser.add_argument('--user', default='root', help='用户名')
    parser.add_argument('--password', default='root', help='密码')
    
    args = parser.parse_args()
    
    config = {
        'host': args.host,
        'port': args.port,
        'database': args.database,
        'user': args.user,
        'password': args.password
    }
    
    success = quick_test(config)
    sys.exit(0 if success else 1)

if __name__ == "__main__":
    main()